package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/6/13.
 */

public class WuZiQiPanel extends View
{
    private int panelWidth;
    private int line=10;
    private float line_height;
    private Paint paint=new Paint();
    private float  bitmap=3/4*1.0f;
    private Bitmap write;
    private Bitmap black;
    private boolean isGameOver=false;
    private boolean isWriteWinner;
    private Context context;
    private List<Point> wlist=new ArrayList<Point>();
    private boolean isW=true;
    private List<Point> blist=new ArrayList<Point>();
    private int MAX_LINE=5;

    public WuZiQiPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        //setBackgroundColor(0x44004400);
        init();
    }

    private void init() {
        paint.setColor(0x88000000);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        write= BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2,null);
        black= BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1,null);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int panelWidth=MeasureSpec.getSize(widthMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int panelHeight=MeasureSpec.getSize(heightMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        int min=Math.min(panelWidth,panelHeight);
        this.panelWidth=min;

        setMeasuredDimension(min,min);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.e("d",line_height*bitmap+"");
        line_height=panelWidth/line;
        Log.e("d",line_height+"");
       int bitmapWidth= (int) (line_height*3/4);
        Log.e("d",bitmapWidth+"");
        write=Bitmap.createScaledBitmap(write,bitmapWidth,bitmapWidth,false);
        black=Bitmap.createScaledBitmap(black,bitmapWidth,bitmapWidth,false);

        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        if(isGameOver)

            return false;

        if(action== MotionEvent.ACTION_UP){
            int x= (int) event.getX();
            int y= (int) event.getY();

            Point point=new Point(x,y);
            point=returnTruePoint(point);
            if(wlist.contains(point)||blist.contains(point)){
                return false;
            }
            if(isW){
                wlist.add(point);
                Log.e("c",point.x+""+point.y);
            }else{
                blist.add(point);
            }
            isW=!isW;

        }
        return true;
    }

    private Point returnTruePoint(Point point) {
        int x= (int) (point.x/line_height);
        int y= (int) (point.y/line_height);
        return new Point(x,y);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);

        drawBitmap(canvas);
        boolean writeWin=checkWinner(wlist);
        boolean blackWin=checkWinner(blist);
        if(writeWin || blackWin){
            isGameOver=true;
            Toast.makeText(context,writeWin?"白棋赢了":"黑棋赢了",Toast.LENGTH_SHORT).show();
        }


    }

    private boolean checkWinner(List<Point> points) {
        for(int i=0;i<points.size();i++){
            int x=points.get(i).x;
            int y=points.get(i).y;

            if(checkHorizontal(x,y,points) ||
            checkVertical(x,y,points)||
            checkLeft(x,y,points)||
            checkRight(x,y,points)){
                return true;
            }
        }
        return  false;
    }

    private boolean checkLeft(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x-i,y-i))){
                count++;
            }else {
                break;
            }
        }
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x+i,y+i))){
                count++;
            }else {
                break;
            }
        }
        if(count>=5){
            return true;
        }else{
            return false;
        }


    }
    private boolean checkRight(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x+i,y-i))){
                count++;
            }else {
                break;
            }
        }
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x-i,y+i))){
                count++;
            }else {
                break;
            }
        }
        if(count>=5){
            return true;
        }else{
            return false;
        }


    }
    private boolean checkVertical(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x,y-i))){
                count++;
            }else {
                break;
            }
        }
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x,y+i))){
                count++;
            }else {
                break;
            }
        }
        if(count>=5){
            return true;
        }else{
            return false;
        }



    }

    private boolean checkHorizontal(int x, int y, List<Point> points) {
        int count=1;
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x-i,y))){
                count++;
            }else {
                break;
            }
        }
        for(int i=1;i<MAX_LINE;i++){
            if(points.contains(new Point(x+i,y))){
                count++;
            }else {
                break;
            }
        }
        if(count>=5){
            return true;
        }else{
            return false;
        }

    }

    private void drawBitmap(Canvas canvas) {
        for(int i=0;i<wlist.size();i++){

            Point point=wlist.get(i);
            float x=  point.x;/*((point.x+(1-bitmap)/2)*line_height);*/
            float y= point.y; /*((point.y+(1-bitmap)/2)*line_height);*/
            canvas.drawBitmap(write,x*line_height,y*line_height,null);

        }
        for(int i=0;i<blist.size();i++){
            Point point=blist.get(i);

            float x=  point.x;/*((point.x+(1-bitmap)/2)*line_height);*/
            float y= point.y; /*((point.y+(1-bitmap)/2)*line_height);*/
            canvas.drawBitmap(black,x*line_height,y*line_height,null);

        }
        invalidate();

    }

    private void drawLine(Canvas canvas) {
        int staxtX=0;

         line_height=panelWidth/line;
        for(int i=0;i<=line;i++){

            canvas.drawLine(staxtX*1.0f,i*line_height*1.0f,staxtX+panelWidth*1.0f,i*line_height*1.0f,paint);
            canvas.drawLine(i*line_height*1.0f,staxtX*1.0f,i*line_height*1.0f,staxtX+panelWidth*1.0f,paint);
        }
    }
}
